package xyz.msj.hrm.properties;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * mq配置类
 */
@Configuration
public class RabbitMQConfig {
    //短信
    public static final String QUEUE_NAME_SMS = "queue_name_sms";
    //邮件
    public static final String QUEUE_NAME_EMAIL = "queue_name_email";
    //交换机  类型通配符
    public static final String QUEUE_NAME_EXCHANGE_TOPIC = "queue_name_exchange_topic";

    //创建交换机  交给spring管理  确定名字
    @Bean(QUEUE_NAME_EXCHANGE_TOPIC)
    public Exchange topicExchange(){
        //设置交换机名字  durable并且进行持久化  ，build最后进行创建
        return ExchangeBuilder.topicExchange(QUEUE_NAME_EXCHANGE_TOPIC).durable(true).build();

    }
    //创建短信队列  交给spring管理  确定名字
    @Bean(QUEUE_NAME_SMS)
    public Queue smsQueue(){
        //设置短信队列名字  durable并且进行持久化
        Queue queue = new Queue(QUEUE_NAME_SMS,true);
        return queue;

    }
    //创建邮箱队列  交给spring管理  确定名字
    @Bean(QUEUE_NAME_EMAIL)
    public Queue emailQueue(){
        //设置短信队列名字  durable并且进行持久化
        Queue queue = new Queue(QUEUE_NAME_EMAIL,true);
        return queue;

    }

    //交换机和短信队列进行绑定  Qualifier  用于指定接收 bean
    @Bean
    public Binding queueNameExchangeTopicWithSmsQueue(
            @Qualifier(QUEUE_NAME_EXCHANGE_TOPIC) Exchange exchange,
            @Qualifier(QUEUE_NAME_SMS) Queue queue){

        return BindingBuilder.bind(queue).to(exchange).with("sms.#").noargs();

    }
    //交换机和邮件队列进行绑定  Qualifier  用于指定接收 bean
    @Bean
    public Binding queueNameExchangeTopicWithEmailQueue(
            @Qualifier(QUEUE_NAME_EXCHANGE_TOPIC) Exchange exchange,
            @Qualifier(QUEUE_NAME_EMAIL) Queue queue){

        return BindingBuilder.bind(queue).to(exchange).with("email.#").noargs();

    }


}
